
export function isHasOwnProperty(obj, attr){
  return Object.prototype.hasOwnProperty.call(obj, attr)
}

export function getCompIdByParam(str){
  let compId = null;
  if(str && str.length > 0){
    let arr = str.split("$");
    if(arr.length > 1){
      compId = arr[1];
    }else{
      return arr[0]
    }
  }
  return compId;
}

export function getRandomArrayElements(arr, notSames, count) {
  let shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
  let min_ = min;
  let isMatch = false;
  while (i-- > min) {
      index = Math.floor((i + 1) * Math.random());
      temp = shuffled[index];
      shuffled[index] = shuffled[i];
      shuffled[i] = temp;
      if(!isMatch && notSames.indexOf(temp) != -1){
        isMatch = true;
        let newArr = _.difference(notSames, [temp]);
        shuffled = _.difference(shuffled, newArr);
        _.forEach(newArr, () => {
          i--;
          min_--;
        })
      }
  }
  return shuffled.slice(min_);
}

export function getQueryString(name) {
  let url = location.search; //获取url中"?"符后的字串
  let theRequest = new Object();
  if (url.indexOf("?") != -1) {
    let str = url.substr(1);
    let strs = str.split("&");
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
  }
  return theRequest[name];
}
export function getCookie(name){
  var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  if (arr != null) return unescape(arr[2]); return "";
}
export function setCookie(name, value, expires, path) {
  delCookie(name);
  expires = 30;
  var exp = new Date();
  path = "/";
  exp.setTime(exp.getTime() + expires * 24 * 60 * 60 * 1000);
  document.cookie = name + "=" + escape(value) + "; expires=" + exp.toGMTString() + "; path=" + path;
}
export function delCookie(name) {
  name = escape(name);
  var path = "/";
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval != null) {
    document.cookie = name + "=" + ";expires=" + exp.toUTCString() + ";path=" + path;
    document.cookie = name + "=" + ";expires=" + exp.toUTCString();
  }
}

export function getMousePos(event) {
  if(!event){
    return { 'x': 0, 'y': 0 };
  }
  var e = event || window.event;
  var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  var x = e.pageX || e.clientX + scrollX;
  var y = e.pageY || e.clientY + scrollY;
  //alert('x: ' + x + '\ny: ' + y);
  return { 'x': x?x:0, 'y': y?y:0 };
}
export function isWeiXin(){
  var ua = window.navigator.userAgent.toLowerCase();
  if(ua.match(/MicroMessenger/i) == 'micromessenger'){
    return true;
  }else{
    return false;
  }
}
export function isWeibo() {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/WeiBo/i) == "weibo") {
    return true
  }else{
    return false
  }
}
export function isQQ(){
  let isQQ = false
  var ua = navigator.userAgent.toLowerCase();
  if(ua.indexOf(' qq') > -1 && ua.indexOf('mqqbrowser') < 0){
    //qq内置浏览器
    isQQ = true
  }
  if(ua.indexOf('mqqbrowser') > -1 && ua.indexOf(" qq") < 0){
    //qq浏览器
    isQQ = true
  }
  return isQQ
}
export function convertDateFormat(){
  var crtTime = new Date();
  return formateDate("yyyy-MM-dd hh:mm:ss", crtTime);//直接调用公共JS里面的时间类处理的办法
}
export function convertDateChineseFormat(date, isHour = true){
  var crtTime = new Date(date.replace(/-/g,  "/"))
  if(isHour){
    return formateDate("yyyy年MM月dd日 hh小时mm分ss秒", crtTime);//直接调用公共JS里面的时间类处理的办法
  }else{
    return formateDate("yyyy年MM月dd日", crtTime);//直接调用公共JS里面的时间类处理的办法
  }
}
function formateDate(fmt, date)
{
  var o = {
    "M+" : date.getMonth()+1,                 //月份
    "d+" : date.getDate(),                    //日
    "h+" : date.getHours(),                   //小时
    "m+" : date.getMinutes(),                 //分
    "s+" : date.getSeconds(),                 //秒
    "q+" : Math.floor((date.getMonth()+3)/3), //季度
    "S"  : date.getMilliseconds()             //毫秒
  };
  if(/(y+)/.test(fmt))
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  return fmt;
}
export function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0,
      v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}
export function testGuid(testID) {
  var reg = new RegExp(/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/);
  if (reg.test(testID)) {
      return true;
  }
  return false;
}
export function replaceStr(formValues, text) {
  //正则
   /* eslint-disable */
  let compIds = text.match(/\$[^\$]+\$/g)
  let value
  let compId_
  text = text.replace(new RegExp(/\$/, 'g'), '')
  _.forEach(compIds, (compId) => {
    compId_ = compId.replace(/\$/g, '')
    // if(formValues[compId_]){
      value = formValues[compId_] ?  formValues[compId_].value : ''
      if(value){
        text = text.replace(new RegExp(compId_, 'g'), value)
      }else{
        text = text.replace(new RegExp(compId_, 'g'), '')
      }
    // }
  })
  return text
}
export function getValueFromXml(text) {
  let xmlDoc;
  try //Internet Explorer
  {
    xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(text);
  } catch (e) {
    try //Firefox, Mozilla, Opera, etc.
    {
      let parser;
      parser = new DOMParser();
      xmlDoc = parser.parseFromString(text, "text/xml");
    } catch (e) {
      // alert(e.message)
    }
  }
  // document.write("xmlDoc is loaded, ready for use");
  return xmlDoc.getElementsByTagName("Location")[0].childNodes[0].nodeValue;
}
export function randomUploadFileName(site_id){
  if(site_id){
    return _.random(100000, 99999999) + "_" + site_id;
  }else{
    return _.random(100000, 99999999);
  }
}

export function getImgRatio(imgW, imgH, scale = 1){
  var w = imgW,
    h = imgH;
  var iw = imgW/scale,
    ih = imgH/scale;
  var xRatio = iw / w,
    yRatio = ih / h,
    sRatio = 1;
  sRatio = Math.min(xRatio, yRatio);
  return sRatio;
}

export function isJSON(str) {
  try {
      let obj = JSON.parse(str);
      return !!obj && typeof obj === 'object';
  } catch (e) {}
  return false;
}


export function getBrowserInfo(){
  var agent = navigator.userAgent.toLowerCase();
  var regStr_ie = /msie [\d.]+;/gi ;
  var regStr_ff = /firefox\/[\d.]+/gi
  var regStr_chrome = /chrome\/[\d.]+/gi ;
  var regStr_saf = /safari\/[\d.]+/gi ;

  if(agent.indexOf("chrome") > 0){
    return agent.match(regStr_chrome) ;
  }else if(agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0){
    return agent.match(regStr_saf) ;
  }else if(agent.indexOf("firefox") > 0){
    return agent.match(regStr_ff) ;
  }else if(agent.indexOf("msie") > 0){
    return agent.match(regStr_ie) ;
  }
}
